{
 +------------------------------------------------------------------------------------------+
 COMPANY:Raven Developers 2008
 +------------------------------------------------------------------------------------------+
 FILE INFO: Currency to words converter PASCAL UNIT, for INR currency ONLY!
 +------------------------------------------------------------------------------------------+
 Portions created by Anirudh K. Mahant are Copyright of Raven Developers (C) 2008.
 +------------------------------------------------------------------------------------------+
 COPYRIGHT NOTICE:
 The contents of this file are protected and copyrighted and are subject to
 the original developer(s) of this file;Unauthorised use of this file is strictly prohibited.

URL:
 http://www.ravendevelopers.com
 +------------------------------------------------------------------------------------------+
 }
unit CurrToWords;

interface

uses
   Classes,
   Controls,
   Dialogs,
   Forms,
   StrUtils,
   SysUtils,
   Variants,
   Windows;

function ConvertCurrencyToEnglish(MyNumber: variant): string;
function ConvertHundreds(MyNumber: variant): string;
function ConvertTens(MyTens: variant): string;
function ConvertDigit(MyDigit: variant): string;

implementation


function ConvertCurrencyToEnglish(MyNumber: variant): string;
var
  Temp: String;
  Ringgits, Cents: String;
  DecimalPlace, Count: Integer;
  Place: array [1..9] of String;
begin
  Place[2] := ' Thousand ';
  Place[3] := ' Million ';
  Place[4] := ' Billion ';
  Place[5] := ' Trillion ';

  // Convert MyNumber to a string, trimming extra spaces.
  MyNumber := Trim(VarToStr(MyNumber));

  // Find decimal place.
  DecimalPlace := AnsiPos('.', MyNumber);

  // If we find decimal place...
  if DecimalPlace > 0 then begin
    // Convert cents
    Temp := LeftStr(MidStr(MyNumber, DecimalPlace + 1, 2) + '00', 2);
    Cents := ConvertTens(Temp);

    // Strip off cents from remainder to convert.
    MyNumber := Trim(LeftStr(MyNumber, DecimalPlace - 1));
  end;

  Count := 1;
  while MyNumber <> '' do begin
    // Convert last 3 digits of MyNumber to English Ringgits.
    Temp := ConvertHundreds(RightStr(MyNumber, 3));
    if Temp <> '' Then Ringgits := Temp + Place[Count] + Ringgits;
    If StrLen(PChar(VarToStr(MyNumber))) > 3 then
    // Remove last 3 converted digits from MyNumber.
      MyNumber := LeftStr(MyNumber, StrLen(PChar(VarToStr(MyNumber))) - 3)
    else
      MyNumber := '';
    Inc(Count);
  end;

  // Clean up Ringgits.
  if Ringgits = '' then Ringgits := 'No Ringgits'
  else if Ringgits = 'One' then Ringgits := 'One Ringgit'
  else Ringgits := Ringgits + ' Ringgits';

  // Clean up cents.
  if Cents = '' then
    //Cents := ' And No Cents'
    Cents := ' Only'
  else if Cents = 'One' then Cents := ' And One Cent'
  else Cents := ' And ' + Cents + ' Cents';

  ConvertCurrencyToEnglish := Ringgits + Cents;
end;

function ConvertHundreds(MyNumber: variant): string;
var
  ResultVal: string;
begin
  // Exit if there is nothing to convert.
  If StrToInt(VarToStr(MyNumber)) = 0 Then exit;

  // Append leading zeros to number.
  MyNumber := RightStr('000' + MyNumber, 3);

  // Do we have a hundreds place digit to convert?
  if LeftStr(MyNumber, 1) <> '0' then
    ResultVal := ConvertDigit(LeftStr(MyNumber, 1)) + ' Hundred ';

  // Do we have a tens place digit to convert?
  if MidStr(MyNumber, 2, 1) <> '0' then
    ResultVal := ResultVal + ConvertTens(MidStr(MyNumber, 2, 2))
  else
    // If not, then convert the ones place digit.
    ResultVal := ResultVal + ConvertDigit(MidStr(MyNumber, 3, 1));

  ConvertHundreds := Trim(ResultVal);
end;

function ConvertTens(MyTens: variant): string;
var
  ResultVal: string;
begin
  // Is value between 10 and 19?
  if StrToInt(LeftStr(VarToStr(MyTens), 1)) = 1 Then
    case StrToInt(VarToStr(MyTens)) of
      10: ResultVal := 'Ten';
      11: ResultVal := 'Eleven';
      12: ResultVal := 'Twelve';
      13: ResultVal := 'Thirteen';
      14: ResultVal := 'Fourteen';
      15: ResultVal := 'Fifteen';
      16: ResultVal := 'Sixteen';
      17: ResultVal := 'Seventeen';
      18: ResultVal := 'Eighteen';
      19: ResultVal := 'Nineteen';
    end
  else begin
  // .. otherwise it's between 20 and 99.
    case StrToInt(LeftStr(VarToStr(MyTens), 1)) of
      2: ResultVal := 'Twenty ';
      3: ResultVal := 'Thirty ';
      4: ResultVal := 'Forty ';
      5: ResultVal := 'Fifty ';
      6: ResultVal := 'Sixty ';
      7: ResultVal := 'Seventy ';
      8: ResultVal := 'Eighty ';
      9: ResultVal := 'Ninety ';
    end;
    // Convert ones place digit.
    ResultVal := ResultVal + ConvertDigit(RightStr(MyTens, 1));
  end;
  ConvertTens := ResultVal;
end;

function ConvertDigit(MyDigit: variant): string;
begin
  case StrToInt(VarToStr(MyDigit)) of
    1: ConvertDigit := 'One';
    2: ConvertDigit := 'Two';
    3: ConvertDigit := 'Three';
    4: ConvertDigit := 'Four';
    5: ConvertDigit := 'Five';
    6: ConvertDigit := 'Six';
    7: ConvertDigit := 'Seven';
    8: ConvertDigit := 'Eight';
    9: ConvertDigit := 'Nine';
    else
      ConvertDigit := '';
  end
end;

end.
